package com.wenshi.web.netty.codec;


import com.wenshi.common.core.domain.ModbusFrame;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ModbusEncoder extends MessageToByteEncoder<ModbusFrame> {
    @Override
    protected void encode(ChannelHandlerContext ctx, ModbusFrame msg, ByteBuf out) throws Exception {
        out.writeShort(msg.getTransactionId());
        out.writeShort(msg.getProtocolId());
        out.writeShort(msg.getLength());
        out.writeByte(msg.getUnitId());
        out.writeByte(msg.getFunctionCode());
        out.writeBytes(msg.getData());
//        ByteBuf copyByteBuf = out.copy();
//        int copyByteBufLength = copyByteBuf.readableBytes();
//        byte[] bytes3 = ByteBufUtils.parseByteCount(copyByteBuf, copyByteBufLength);
//        String hexString = bytesToHexString(bytes3);
//        log.info("发送的报文:{}", hexString);
    }
}
